// pages/comment/comment.js
import {
  BASE_URL
} from '../../api/index'
import {
  addComment
} from '../../api/evaluation'
Page({
  data: {
    one_1: 5,
    two_1: 0,
    one_2: 5,
    two_2: 0,
    one_3: 5,
    two_3: 0,
    files: [],
    goodsId: '',
    skuId: '',
    orderItemSn: '',
    name: '',
    img: '',
    content: ''
  },
  onLoad: function (e) {
    console.log(e)
    this.setData({
      goodsId: e.goodsId,
      skuId: e.skuId,
      orderItemSn: e.orderItemSn,
      name: e.name,
      img: e.img
    })
  },
  // 添加图片
  chooseImage: function (e) {
    var that = this;
    var images = that.data.files;
    wx.chooseMedia({
      count: 1, // 默认9
      mediaType: ['image'],
      sizeType: ['original'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFiles[0].tempFilePath
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        wx.showLoading({
          title: '上传中...',
          mask: true
        })
        console.log( BASE_URL + 'system/common/file/upload')
        wx.uploadFile({
          url: BASE_URL + 'system/common/file/upload', //接受图片的接口地址
          filePath: tempFilePaths,
          name: 'file',
          success(res) {
            let resp = JSON.parse(res.data)
            that.setData({
              files: that.data.files.concat(resp.data)
            })
          },
          complete() {
            wx.hideLoading()
          }
        })
      }
    })
  },
  // 预览图片
  previewImage(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id,
      urls: e.currentTarget.dataset.urls
    })
  },
  //长按图片删除
  deleteImage: function (e) {
    var that = this;
    var images = that.data.files;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    wx.showModal({
      title: '提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          files: images
        });
      }
    })
  },
  // 图片删除效果
  closeClick: function (e) {
    var that = this;
    var images = that.data.files;
    var index = e.currentTarget.dataset.index; //获取当前按图片下标
    console.log(index)
    wx.showModal({
      title: '系统提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          files: images
        });
      }
    })
  },
  shangpin_in: function (e) {
    var shangpin_in = e.currentTarget.dataset.in;
    var one_1;
    if (shangpin_in === 'use_sc1') {
      one_1 = Number(e.currentTarget.id);
    } else {
      one_1 = Number(e.currentTarget.id) + this.data.one_1;
    }
    this.setData({
      one_1: one_1,
      two_1: 5 - one_1,
    })
    console.log(one_1)
  },

  maijia_in: function (e) {
    var maijia_in = e.currentTarget.dataset.in;
    var one_2;
    if (maijia_in === 'use_sc2') {
      one_2 = Number(e.currentTarget.id);
    } else {
      one_2 = Number(e.currentTarget.id) + this.data.one_2;
    }
    this.setData({
      one_2: one_2,
      two_2: 5 - one_2
    })
    console.log(one_2)
  },

  wuliu_in: function (e) {
    var wuliu_in = e.currentTarget.dataset.in;
    var one_3;
    if (wuliu_in === 'use_sc3') {
      one_3 = Number(e.currentTarget.id);
    } else {
      one_3 = Number(e.currentTarget.id) + this.data.one_3;
    }
    this.setData({
      one_3: one_3,
      two_3: 5 - one_3
    })
    console.log(one_3)
  },
  inputContent(e) {
    this.setData({
      content: e.detail.value
    })
  },
  async subComment() {
    if (this.data.content == '') {
      wx.showToast({
        title: '请输入评价内容',
        icon: 'none'
      })
      return
    }
    let data = {
      content: this.data.content,
      deliveryScore: this.data.one_3,
      descriptionScore: this.data.one_1,
      goodsId: this.data.goodsId,
      grade: this.data.one_1 > 3 ? 'GOOD' : this.data.one_1 > 1 ? 'MODERATE' : 'WORSE',
      images: this.data.files.length > 0 ? this.data.files.join(',') : '',
      orderItemSn: this.data.orderItemSn,
      serviceScore: this.data.one_2,
      skuId: this.data.skuId
    }
    console.log(data)
    let res = await addComment(data, wx.getStorageSync('access_token'))
    if (res && res.code == 10000) {
      wx.showToast({
        title: '评价成功！',
        icon: 'success',
        duration: 1500,
        success: () => {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1 //返回上一级页面
            })
          }, 1500)
        }
      })
    }
  },
})